import 'package:palmf_hid/common/entities/response/lot_sample_entity.dart';
import 'package:palmf_hid/common/utils/utils.dart';
import 'package:hex/hex.dart';
import 'common_order_util.dart';

//合模校验: 取值从指令头55AA后到指令倒数第二字节进行和模校验，数据放在整条指令的最后一个字节
const VENDER_ID = 18505;
const PRODUCT_ID = 2056;
const USB_DEVICE_ATTACHED = 'android.hardware.usb.action.USB_DEVICE_ATTACHED';
const USB_DEVICE_DETACHED = 'android.hardware.usb.action.USB_DEVICE_DETACHED';
//指令长度
const ORDER_LEN = 64;
//握手
const ORDER_HEAD_HANDLE = "55AA01";
//获取仪器编号
const ORDER_HEAD_GET_DEVICE_NO = "55AA0F";
//同步数据
const ORDER_HEAD_SYNC_DATA_SEND = "55AA09";
//结果返回
const ORDER_HEAD_REUSLT_BACK = "55AA09";
//删除测试结果
const ORDER_HEAD_DELETE_RESULT = "55AA0D";
//获取当前时间
const ORDER_HEAD_GET_DEVICE_TIME = "55AA15";
//设置时间
const ORDER_HEAD_SET_DEVICE_TIME = "55AA17";
// 清除二维码
const ORDER_HEAD_CLEAR_QR_CODE = "55AA18";
//测试完成
const ORDER_HEAD_TEST_OVER = "55AA62";
//插入卡
const ORDER_HEAD_CARD_IN = "55AA4B";
//拔出卡
const ORDER_HEAD_CARD_OUT = "55AA4C";
//获取未同步结果总条数
const ORDER_HEAD_UNLOAD_SYNC_DATA_NUM = "55AA21";
//获取未同步结果总条数 返回没有需要同步的数据
const ORDER_HEAD_SYNC_DATA_NUM_NO_DATA = "55AA59";
//收到测试结果需要给手持机返回的指令
const ORDER_HEAD_CHECK_OVER_SEND = "55AA22";
//下发参考范围
const ORDER_HEAD_SET_PARAM = "55AA0C";
//获取手持机测试总数量
const ORDER_HEAD_GET_ALL_TEST_NUM = "55AA23";
//根据用户输入同步测试数据
const ORDER_HEAD_GET_TEST_BY_USER = "55AA24";
//根据用户输入同步测试数量获取手持机测试总数量
const ORDER_HEAD_GET_TEST_BY_USER_NUM = "55AA26";
//根据用户输入同步测试数据,返回沒有
const ORDER_HEAD_GET_TEST_BY_USER_BACK_NO_DATA = "55AA25";

//十六进制字符串拼接成指令
List<int> generateOrderHex(String head, String data) {
  List<int> orderArr = [];
  List<int> headArr = HEX.decode(head);
  orderArr.addAll(headArr);
  if (data != "") {
    List<int> dataArr = HEX.decode(data);
    orderArr.addAll(dataArr);
  }
  if (orderArr.length != 63) {
    String lastStr = "";
    for (int i = 0; i < (63 - orderArr.length); i++) {
      lastStr += "00";
    }
    orderArr.addAll(HEX.decode(lastStr));
  }
  int checkSum = calCheckSum(orderArr.sublist(2));
  orderArr.add(checkSum);
  String dataStr = checkSum.toRadixString(16);
  if (dataStr.length < 2) {
    dataStr = "0" + dataStr;
  }

  List radixData = [];
  for (var i = 0; i < orderArr.length; i++) {
    String dataStr = orderArr[i].toRadixString(16);
    if (dataStr.length < 2) {
      dataStr = "0" + dataStr;
    }
    String dataEndStr = dataStr;
    radixData.add(dataEndStr);
  }
  MyLogUtil().i("发送的十六进制指令：$radixData---int数据-$orderArr");
  return orderArr;
}

//生成下发某一批号试剂卡指令
List<int> generateOrderOfSetReagent(
    String head, List<String> param, List<int> data) {
  if (param.isNotEmpty) {
    if (param.length != 12) {
      int len = param.length;
      for (int i = 0; i < 12 - len; i++) {
        param.add("0");
      }
    }

    String para3 = param[3] == "0" ? "00000000" : param[3];
    String para2 = param[2] == "0" ? "00000000" : param[2];
    String para1 = param[1] == "0" ? "00000000" : param[1];
    String para0 = param[0] == "0" ? "00000000" : param[0];

    String para7 = param[7] == "0" ? "00000000" : param[7];
    String para6 = param[6] == "0" ? "00000000" : param[6];
    String para5 = param[5] == "0" ? "00000000" : param[5];
    String para4 = param[4] == "0" ? "00000000" : param[4];

    String para11 = param[11] == "0" ? "00000000" : param[11];
    String para10 = param[10] == "0" ? "00000000" : param[10];
    String para9 = param[9] == "0" ? "00000000" : param[9];
    String para8 = param[8] == "0" ? "00000000" : param[8];

    List<int> a3 = HEX.decode(para3).reversed.toList();
    List<int> a2 = HEX.decode(para2).reversed.toList();
    List<int> a1 = HEX.decode(para1).reversed.toList();
    List<int> a0 = HEX.decode(para0).reversed.toList();

    List<int> a7 = HEX.decode(para7).reversed.toList();
    List<int> a6 = HEX.decode(para6).reversed.toList();
    List<int> a5 = HEX.decode(para5).reversed.toList();
    List<int> a4 = HEX.decode(para4).reversed.toList();

    List<int> a11 = HEX.decode(para11).reversed.toList();
    List<int> a10 = HEX.decode(para10).reversed.toList();
    List<int> a9 = HEX.decode(para9).reversed.toList();
    List<int> a8 = HEX.decode(para8).reversed.toList();

    data.replaceRange(27, 31, a3);
    data.replaceRange(31, 35, a2);
    data.replaceRange(35, 39, a1);
    data.replaceRange(39, 43, a0);

    data.replaceRange(52, 56, a7);
    data.replaceRange(56, 60, a6);
    data.replaceRange(60, 64, a5);
    data.replaceRange(64, 68, a4);

    data.replaceRange(77, 81, a11);
    data.replaceRange(81, 85, a10);
    data.replaceRange(85, 89, a9);
    data.replaceRange(89, 93, a8);
  }

  List<int> headArr = HEX.decode(head);
  List<int> orderArr = [];
  for (int i = 0; i < 4; i++) {
    List<int> temp = [];

    temp.addAll(headArr);
    if(i==3){
      List<int> headArr = HEX.decode('1d');
      temp.addAll(headArr);
    }else{
      List<int> headArr = HEX.decode('39');
      temp.addAll(headArr);
    }
    temp.addAll(data.sublist(i * 57, i * 57 + 57));
    temp.add(i + 1);
    temp.add(4);
    orderArr.addAll(temp);
    if (orderArr.length != 63) {
      String lastStr = "";
      for (int i = 0; i < (63 - orderArr.length); i++) {
        lastStr += "00";
      }
      orderArr.addAll(HEX.decode(lastStr));
    }
    orderArr.add(calCheckSum(temp.sublist(2)));
  }
  MyLogUtil().i("生成下发某一批号试剂卡指令：${intArrToHex(orderArr)}}");
  // MyLogUtil().i(
  //     "生成下发某一试剂信息指令：${orderArr.length.toString()}  ${orderArr.toString()} ${param.toString()}");
  return orderArr;
}

//生成获取某一批号试剂卡指令
List<int> generateOrderOfGetReagentByLotNum(
    String head, LotSampleEntity lotSampleEntity) {
  /***
   * 1字节顺序号，从0开始 数字
      1字节试剂卡标识,ASCII
      10字节批号，ASCII
      1字节样本类型，0：全血1：血清 数字
   */
  List<int> orderArr = [];
  List<int> headArr = HEX.decode(head);
  int markNum = int.parse(lotSampleEntity.cardMark!);
  String lotNum = lotSampleEntity.lotNum!;
  List<int> lotNumArr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

  List<int> temp = HEX.decode(toRadixStr(lotNum));
  lotNumArr.replaceRange(0, temp.length, temp);
  int sampleType = int.parse(lotSampleEntity.sampleType!);

  orderArr.addAll(headArr);
  orderArr.add(markNum);
  orderArr.addAll(lotNumArr);
  orderArr.add(sampleType);
  if (orderArr.length != 63) {
    String lastStr = "";
    for (int i = 0; i < (63 - orderArr.length); i++) {
      lastStr += "00";
    }

    orderArr.addAll(HEX.decode(lastStr));
  }
  int check = calCheckSum(orderArr.sublist(2));
  //添加校验位
  orderArr.add(check);
  List radixData = [];
  for (var i = 0; i < orderArr.length; i++) {
    String dataStr = orderArr[i].toRadixString(16);
    if (dataStr.length < 2) {
      dataStr = "0" + dataStr;
    }
    String dataEndStr = dataStr;
    radixData.add(dataEndStr);
  }
  MyLogUtil().i("获取某一试剂卡数据十六进制 - $radixData");
  return orderArr;
}
